home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d19 / cal14s13.arc / QREAD.ASM < prev    next >
Assembly Source File  |  1989-04-08  |  4KB  |  179 lines

  1.  
  2.         page ,132
  3. ;
  4. ; Qread - quick version of ReadLn for text files
  5. ;         Used by Qread unit.
  6. ;
  7. ; Written by Samuel Smith, 11-19-88
  8. ;
  9.  
  10. code segment byte public
  11.         assume cs:code
  12.         public qReadLn
  13.  
  14. ; -------------------------------------------------------------
  15. ;
  16. ; structure of text file record
  17. ;
  18. textfile struc
  19.         handle  dw ?    ;dos handle
  20.         mode    dw ?    ;open mode
  21.         bufSize dw ?    ;size of file buffer
  22.         priv1   dw ?
  23.         bufPos  dw ?    ;position of next read within file buffer
  24.         bufEnd  dw ?    ;position past last byte of file buffer
  25.         bufPtr  dd ?    ;pointer to file file buffer
  26. textfile ends
  27.  
  28.         
  29. ; -------------------------------------------------------------
  30. ;
  31. ; procedure qReadLn( var fd: text;
  32. ;                    var dest: string;
  33. ;                    maxlen: word );
  34. ;
  35.  
  36. ; structure of stack frame --
  37. fileptr equ dword ptr [bp+0ch]  ;pointer to text file record
  38. destptr equ dword ptr [bp+08h]  ;pointer to destination string
  39. maxlen  equ word  ptr [bp+06h]  ;maximum length of destination string
  40. curpos  equ dx
  41. curend  equ bx
  42.  
  43. qReadLn proc far
  44.         push bp
  45.         mov bp,sp               ;create stack frame
  46.         push ds
  47.  
  48.         lds si,fileptr          ;ds:si -> textfile
  49.         mov curpos,bufpos[si]   ;current file buffer position
  50.         mov curend,bufend[si]   ;end of file buffer position
  51.  
  52.         lds si,bufptr[si]       ;ds:si -> file buffer
  53.         add si,curpos           ;            [bufpos]
  54.  
  55.         les di,destptr          ;es:di -> dest
  56.         inc di
  57.  
  58.         mov cx,maxlen           ;initial destination space
  59.         jmp short NextChar      ;get first character
  60.  
  61. ;
  62. ; main character loop
  63. ;
  64. StoreChar:
  65.         stosb                   ;dest[len++] = c
  66.  
  67. ; process next character in file buffer
  68. NextChar:
  69.         cmp curpos,curend       ;end of file buffer?
  70.         jz NextBuffer
  71.  
  72. ; have a character in the file buffer - get it
  73. HaveChar:
  74.         lodsb                   ;c = buf[bufptr++]
  75.         inc curpos
  76.  
  77.         cmp al,26
  78.         jle CheckControl
  79.  
  80. ; it is a normal character - add it to the destination buffer
  81. NormChar:
  82.         loop StoreChar          ;dec cx, jnz
  83.         jmp short qEndLine
  84.  
  85. ;
  86. ; check control characters
  87. ;
  88. CheckControl:
  89.         cmp al,26               ;^Z? end of file
  90.         jz qEndFile
  91.         cmp al,10               ;lf? end of line
  92.         jz qEndLine
  93.         cmp al,13               ;cr? skip it
  94.         jz NextChar
  95.         jmp short NormChar
  96.  
  97. ;
  98. ; file buffer is empty - get another one
  99. ;
  100. NextBuffer:
  101.         lds si,fileptr          ;ds:si -> textfile
  102.         call qFillBuf
  103.  
  104.         mov curend,bufend[si]
  105.         xor curpos,curpos       ;bufpos=0
  106.         lds si,bufptr[si]       ;ds:si -> file buffer
  107.  
  108.         cmp curend,curpos
  109.         jnz HaveChar
  110.  
  111. ;
  112. ; end of file - return dest = ^Z unless dest has data in it
  113. ;
  114. qEndFile:
  115.         cmp cx,maxlen
  116.         jnz qEndLine
  117.  
  118.         mov al,26
  119.         stosb                   ;dest = ^Z
  120.         dec cx
  121.  
  122. ;
  123. ; end of line - set line length and return
  124. ;
  125. qEndLine:
  126.         lds si,fileptr          ;ds:si -> textfile
  127.         mov bufpos[si],curpos   ;update file buffer position
  128.  
  129.         mov ax,maxlen
  130.         sub ax,cx               ;calculate destination bytes used
  131.         les di,destptr          ;es:di -> dest
  132.         mov es:[di],al          ;update destination length
  133.  
  134.         pop ds
  135.         pop bp
  136.         ret 10                  ;dispose of 10 parameter bytes
  137. qReadLn endp
  138.  
  139.  
  140. ; -------------------------------------------------------------
  141. ;
  142. ; fill file buffer
  143. ;
  144. ; from  handle[si]      dos handle
  145. ;       bufptr[si]      data buffer
  146. ;       bufsize[si]     data buffer size
  147. ;
  148. ; sets  bufpos[si]      to 0
  149. ;       bufend[si]      to bytes read
  150. ;
  151. ; preserves dx, cx, bx, ds, es, si, di
  152. ;
  153.  
  154. qFillBuf proc near
  155.         push dx
  156.         push cx
  157.         push bx
  158.  
  159.         mov ax,3f00h            ;read
  160.         mov bx,handle[si]       ;dos handle
  161.         mov cx,bufsize[si]      ;file buffer size
  162.         push ds
  163.         lds dx,bufptr[si]       ;file buffer pointer
  164.         int 21h                 ;perform the read
  165.         pop ds
  166.         mov bufend[si],ax       ;bytes read
  167.         mov bufpos[si],0        ;bytes used
  168.  
  169.         pop bx
  170.         pop cx
  171.         pop dx
  172.         ret
  173. qFillBuf endp
  174.  
  175. code ends
  176. end
  177.  
  178.  
  179.